
(function (window) {

KC.extend({
	SINGLE_TAGS : 'br,hr,img,area,col,embed,input,param',
	INLINE_TAGS : 'b,del,em,font,i,span,strike,strong,sub,sup,u,img,embed'
});

KC.extend({
	index : function(node) {
		var i = 0;
		while (node) {
			node = node.previousSibling;
			i++;
		}
		return --i;
	},
	html : function(el) {
		var html = el.innerHTML.toLowerCase();
		html = html.replace(/\n/g, '');
		html = KC.formatHtml(html);
		return html;
	},
	isAncestor : function(ancestor, node) {
		while (node = node.parentNode) {
			if (node === ancestor) return true;
		}
		return false;
	},
	isSingle : function(el) {
		return (',' + KC.SINGLE_TAGS + ',').indexOf(',' + el.nodeName.toLowerCase() + ',') > -1;
	},
	isInline : function(el) {
		return (',' + KC.INLINE_TAGS + ',').indexOf(',' + el.nodeName.toLowerCase() + ',') > -1;
	},
	attr : function(el, key) {
		key = key.toLowerCase();
		var val = null;
		if (KC.browser.IE && KC.browser.VERSION < 8) {
			var div = el.ownerDocument.createElement('div');
			div.appendChild(el.cloneNode(false));
			var re = /\s+(?:([\w-:]+)|(?:([\w-:]+)=([^\s"'<>]+))|(?:([\w-:]+)="([^"]*)")|(?:([\w-:]+)='([^']*)'))(?=(?:\s|\/|>)+)/g;
			var arr, k, v, list = {};
			while ((arr = re.exec(div.innerHTML.toLowerCase()))) {
				k = arr[1] || arr[2] || arr[4] || arr[6];
				v = arr[1] || (arr[2] ? arr[3] : (arr[4] ? arr[5] : arr[7]));
				if (k === key) {
					val = v;
					break;
				}
			}
		} else {
			val = el.getAttribute(key, 2);
		}
		if (key === 'style' && val !== null) {
			val = KC.formatStyle(val);
		}
		return val;
	},
	eachNode : function(node, func, order) {
		order = (typeof order == 'undefined') ? true : order;
		function walk(parent) {
			if (!parent) return;
			var n = order ? parent.firstChild : parent.lastChild;
			if (!n) return;
			while (n) {
				var next = order ? n.nextSibling : n.previousSibling;
				if (func(n)) return true;
				if (walk(n)) return true;
				n = next;
			}
		};
		if (func(node)) return true;
		walk(node);
	},
	trimNode : function(node) {
		var doc = node.ownerDocument;
		function ltrim(str) {
			return str.replace(/^\s+/g, ''); 
		}
		function rtrim(str) {
			return str.replace(/\s+$/g, ''); 
		}
		var text, parent, prev, next, isRightBlank = false;
		KC.eachNode(node, function(n) {
			parent = n.parentNode;
			if (n.nodeType != 3) return;
			prev = n.previousSibling;
			next = n.nextSibling;
			text = n.nodeValue;
			if (prev && !KC.isInline(prev)) text = ltrim(text);
			if (next && !KC.isInline(next)) text = rtrim(text);
			if (!parent.previousSibling && !KC.isInline(parent.parentNode) && (!prev || !KC.isInline(prev))) text = ltrim(text);
			if (!parent.nextSibling && !KC.isInline(parent.parentNode) && (!next || !KC.isInline(next))) text = ltrim(text);
			if (isRightBlank) text = ltrim(text);
			text = text.replace(/\s+/g, ' ');
			isRightBlank = /\s+$/.test(text);
			if (KC.trim(text) === '') parent.removeChild(n);
			else parent.replaceChild(doc.createTextNode(text), n);
		});
	}
});

})(window);

